#
# All information at your chilly fingertips.
#
# https://gist.github.com/TomFrost/9472926
#
# Authors:
#   Tom Frost <tom@frosteddesign.com>
#   Stefano Mazzucco <stefano@curso.re>
#
# Screenshots:
#   http://i.tomfro.st/U1Kov.png
#

# Dependencies
pmodload 'helper'

function prompt_frost_precmd {
  setopt LOCAL_OPTIONS
  unsetopt XTRACE KSH_ARRAYS

  if (( $+functions[git-info] )); then
    git-info
  fi

  if (( $+functions[python-info] )); then
    python-info
  fi

  if (( $+functions[battery-info] )); then
    battery-info
  fi
}

function prompt_frost_setup {
  setopt LOCAL_OPTIONS
  unsetopt XTRACE KSH_ARRAYS
  prompt_opts=(cr percent subst)

  # Load required functions.
  autoload -Uz add-zsh-hook
  autoload -Uz vcs_info

  # Add hook for calling vcs_info before each command.
  add-zsh-hook precmd prompt_frost_precmd

  # Tell prezto we can manage this prompt
  zstyle ':prezto:module:prompt' managed 'yes'

  _prompt_frost_nocolor="%{$terminfo[sgr0]%}%f"
  typeset -A _prompt_frost_colors
  _prompt_frost_colors[normalcyan]="${_prompt_frost_nocolor}%F{cyan}"
  _prompt_frost_colors[boldblue]="${_prompt_frost_nocolor}%F{blue}%{$terminfo[bold]%}"
  _prompt_frost_colors[normalgreen]="${_prompt_frost_nocolor}%F{green}"
  _prompt_frost_colors[boldgreen]="${_prompt_frost_nocolor}%F{green}%{$terminfo[bold]%}"
  _prompt_frost_colors[normalred]="${_prompt_frost_nocolor}%F{red}"
  _prompt_frost_colors[boldred]="${_prompt_frost_nocolor}%F{red}%{$terminfo[bold]%}"
  _prompt_frost_colors[normalyellow]="${_prompt_frost_nocolor}%F{yellow}"
  _prompt_frost_colors[boldyellow]="${_prompt_frost_nocolor}%F{yellow}%{$terminfo[bold]%}"

  # Set editor-info params
  zstyle ':prezto:module:editor:info:completing' format '${_prompt_frost_colors[normalred]}...${_prompt_frost_nocolor}'

  local prim
  [[ $TERM = "xterm"* ]] && prim='➤' || prim='▶'
  zstyle ':prezto:module:editor:info:keymap:primary' format $prim

  local ovr
  [[ $TERM = "xterm"* ]] && ovr='♺' || ovr='◘'
  zstyle ':prezto:module:editor:info:keymap:primary:overwrite' format $ovr

  local alt
  [[ $TERM = "xterm"* ]] && alt='⍟' || alt='○'
  zstyle ':prezto:module:editor:info:keymap:alternate' format $alt

  # Set get-info parameters
  zstyle ':prezto:module:git:info' verbose 'yes'
  zstyle ':prezto:module:git:info:action' format "${_prompt_frost_colors[normalcyan]}∙${_prompt_frost_colors[normalyellow]}%s${_prompt_frost_nocolor}"

  local gitadded
  [[ $TERM = "xterm"* ]] && gitadded='✚' || gitadded='┼'
  zstyle ':prezto:module:git:info:added' format "${_prompt_frost_colors[boldgreen]} $gitadded${_prompt_frost_nocolor}"

  local gitahead
  [[ $TERM = "xterm"* ]] && gitahead='⚡' || gitahead='▲'
  zstyle ':prezto:module:git:info:ahead' format "${_prompt_frost_colors[boldyellow]} $gitahead${_prompt_frost_nocolor}"

  local gitbehind
  [[ $TERM = "xterm"* ]] && gitbehind='↯' || gitbehind='▼'
  zstyle ':prezto:module:git:info:behind' format "${_prompt_frost_colors[boldyellow]} ↯${_prompt_frost_nocolor}"

  zstyle ':prezto:module:git:info:branch' format "${_prompt_frost_colors[normalcyan]}∙${_prompt_frost_colors[boldyellow]}%b${_prompt_frost_nocolor}"

  zstyle ':prezto:module:git:info:tag' format "${_prompt_frost_colors[normalcyan]}∙${_prompt_frost_colors[normalred]}%t${_prompt_frost_nocolor}"

  local gitclean
  [[ $TERM = "xterm"* ]] && gitclean='✔' || gitclean='≡'
  zstyle ':prezto:module:git:info:clean' format "${_prompt_frost_colors[normalgreen]}$gitclean${_prompt_frost_nocolor}"

  zstyle ':prezto:module:git:info:commit' format "${_prompt_frost_colors[boldblue]}%.7c${_prompt_frost_nocolor}"

  local gitdeleted
  [[ $TERM = "xterm"* ]] && gitdeleted='✖' || gitdeleted='‼'
  zstyle ':prezto:module:git:info:deleted' format "${_prompt_frost_colors[boldred]} $gitdeleted${_prompt_frost_nocolor}"

  local gitmodified
  [[ $TERM = "xterm"* ]] && gitmodified='✱' || gitmodified='≈'
  zstyle ':prezto:module:git:info:modified' format "${_prompt_frost_colors[normalyellow]} $gitmodified${_prompt_frost_nocolor}"

  local gitrenamed
  [[ $TERM = "xterm"* ]] && gitrenamed='➜' || gitrenamed='→'
  zstyle ':prezto:module:git:info:renamed' format "${_prompt_frost_colors[boldred]} $gitrenamed${_prompt_frost_nocolor}"

  local gitstashed
  [[ $TERM = "xterm"* ]] && gitstashed='✭' || gitstashed='↨'
  zstyle ':prezto:module:git:info:stashed' format "${_prompt_frost_colors[normalyellow]} $gitstashed${_prompt_frost_nocolor}"

  local gitunmerged
  [[ $TERM = "xterm"* ]] && gitunmerged='═' || gitunmerged='='
  zstyle ':prezto:module:git:info:unmerged' format "${_prompt_frost_colors[normalyellow]} $gitunmerged${_prompt_frost_nocolor}"

  local gituntracked
  [[ $TERM = "xterm"* ]] && gituntracked='▪' || gituntracked='▓'
  zstyle ':prezto:module:git:info:untracked' format "${_prompt_frost_colors[boldred]} $gituntracked${_prompt_frost_nocolor}"

  zstyle ':prezto:module:git:info:keys' format \
    'status' "${_prompt_frost_colors[normalcyan]}<%c%b%t%s${_prompt_frost_colors[normalcyan]}∙%C%A%B%S%a%d%m%r%U%u${_prompt_frost_colors[normalcyan]}>-"

  # Set python-info parameters
  zstyle ':prezto:module:python:info:virtualenv' format "${_prompt_frost_colors[boldred]}[venv:${_prompt_frost_colors[normalred]}%v${_prompt_frost_colors[boldred]}]${_prompt_frost_colors[normalcyan]}-"

  # Set battery-info parameters
  zstyle ':prezto:module:battery:info:percent' format "${_prompt_frost_colors[boldblue]}%p${_prompt_frost_nocolor}"

  zstyle ':prezto:module:battery:info:red' format "${_prompt_frost_colors[normalred]}▂${_prompt_frost_nocolor}"
  zstyle ':prezto:module:battery:info:yellow' format "${_prompt_frost_colors[normalyellow]}▄${_prompt_frost_nocolor}"
  zstyle ':prezto:module:battery:info:green' format "${_prompt_frost_colors[normalgreen]}█${_prompt_frost_nocolor}"

  zstyle ':prezto:module:battery:info:arrow_up' format "${_prompt_frost_colors[normalyellow]}↑${_prompt_frost_nocolor}"
  zstyle ':prezto:module:battery:info:arrow_down' format "${_prompt_frost_colors[normalyellow]}↓${_prompt_frost_nocolor}"

  zstyle ':prezto:module:battery:info:keys' format \
         'status' "${_prompt_frost_colors[normalcyan]}<%r%y%g%u%d%p${_prompt_frost_colors[normalcyan]}>"

  # Get the time
  local time="${_prompt_frost_colors[boldblue]}%T"

  # Remote hosts change colors
  if [[ -n "$SSH_CLIENT"  ||  -n "$SSH2_CLIENT" ]]; then
    local host="${_prompt_frost_colors[boldyellow]}%m" #SSH
  else
    local host="${_prompt_frost_colors[boldgreen]}%m" # no SSH
  fi

  local user="%(!.${_prompt_frost_colors[boldred]}.${_prompt_frost_colors[boldgreen]})%n"
  local promptcolor='%(!.${_prompt_frost_colors[boldred]}.${_prompt_frost_nocolor})'

  local diamond
  [[ $TERM = "xterm"* ]] && diamond="◇" || diamond="◆"
  diamond="${_prompt_frost_colors[normalcyan]}$diamond"
  local curveup
  [[ $TERM = "xterm"* ]] && curveup="╭" || curveup="┌"
  curveup="${_prompt_frost_colors[normalcyan]}$curveup"
  local curvedown
  [[ $TERM = "xterm"* ]] && curvedown="╰" || curvedown="└"
  curvedown="${_prompt_frost_colors[normalcyan]}$curvedown"
  local connector="${_prompt_frost_colors[normalcyan]}>-<"
  local open_bracket="${_prompt_frost_colors[normalcyan]}<"
  local close_bracket="${_prompt_frost_colors[normalcyan]}>"
  local dash="${_prompt_frost_colors[normalcyan]}-"
  local path="${_prompt_frost_colors[boldblue]}%~"
  local at="${_prompt_frost_colors[normalcyan]}@"
  local editr='%(?.${editor_info[keymap]}.%F{red} %?${editor_info[keymap]})'
  [[ -z $IN_NIX_SHELL ]] || local _prompt_frost_nix_shell="${_prompt_frost_colors[boldred]}[nix:${_prompt_frost_colors[normalred]}${name:-shell}${_prompt_frost_colors[boldred]}]${_prompt_frost_nocolor}${dash}"
  # Define prompts.
  PROMPT="
${curveup}${dash}${open_bracket}${user}${at}${host}${connector}${path}${close_bracket}${dash}"${_prompt_frost_nix_shell}'$python_info[virtualenv]'""'${git_info[status]}'"${diamond}
${curvedown}${dash}${promptcolor}"${editr}"${_prompt_frost_nocolor} "
  RPROMPT="%{$(echotc UP 1)%}${open_bracket}$time${close_bracket}${dash}"'${battery_info[status]}'"%{$(echotc DO 1)%}${_prompt_frost_nocolor}"
  SPROMPT='zsh: correct %F{red}%R%f to %F{green}%r%f [nyae]? '
}

prompt_frost_setup "$@"
